package icursoft.educationms.ui.user;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.callback.StringCallback;

import icursoft.educationms.BaseActivity;
import icursoft.educationms.EducationMSApplication;
import icursoft.educationms.R;
import icursoft.educationms.http.HttpResult;
import icursoft.educationms.servive.PreferencesService;
import icursoft.educationms.util.CommUtil;
import icursoft.educationms.util.Const;
import icursoft.educationms.util.MachTalkUtil;


public class UpdatePwdActivity extends BaseActivity {
    private final static String TAG = UpdatePwdActivity.class.getSimpleName();
    private EditText et_oldPass;
    private EditText et_newPass;
    private EditText et_reNewPass;
    String oldPass = null;
    String newPass1 = null;
    String newPass2 = null;
    View focusView = null;
    Button btnsave;
    boolean flag_oldpwd_notnull = false;
    boolean flag_newpwd_notnull = false;
    boolean flag_confimpwd_notnull = false;
    int FLAG_OLDPWD = 1;
    int FLAG_NEWPWD = 2;
    int FLAG_CONFIMPWD = 3;
    LinearLayout ll_oldpwd_delete;
    LinearLayout ll_newpwd_delete;
    LinearLayout ll_confimpwd_delete;
    TextView title_tv;

//    @Override
//    public void onTokenShortSuccessLogicAgain() {
//        MachTalkUtil.getInstance(mContext).updateDoctorPwd(callback, PreferencesService.getInstance(mContext).getUserId(),
//                et_oldPass.getText().toString(), et_newPass.getText().toString());
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_pwd;
    }


    @Override
    protected void setupViews() {
        title_tv = getView(R.id.title_tv);
        title_tv.setText(getResources().getString(R.string.change_password));
        getView(R.id.left_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ll_oldpwd_delete = (LinearLayout) findViewById(R.id.ll_pwd_delete);
        ll_newpwd_delete = (LinearLayout) findViewById(R.id.ll_npwd_delete);
        ll_confimpwd_delete = (LinearLayout) findViewById(R.id.ll_cpwd_delete);
        et_oldPass = (EditText) findViewById(R.id.register_edittext_num);
        et_newPass = (EditText) findViewById(R.id.password_edittext_password);
        et_reNewPass = (EditText) findViewById(R.id.password_edittext_password_ensure);
        btnsave = (Button) findViewById(R.id.image_button_change_password);
        btnsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                attemptChange();
            }
        });
        btnsave.setBackgroundResource(R.mipmap.button_gray);
        btnsave.setClickable(false);
        et_oldPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View arg0, boolean isFocuse) {
                // TODO Auto-generated method stub
                if (isFocuse && flag_oldpwd_notnull) {
                    ll_oldpwd_delete.setVisibility(View.VISIBLE);
                } else {
                    ll_oldpwd_delete.setVisibility(View.GONE);
                }
            }
        });
        et_newPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View arg0, boolean isFocuse) {
                // TODO Auto-generated method stub
                if (isFocuse && flag_newpwd_notnull) {
                    ll_newpwd_delete.setVisibility(View.VISIBLE);
                } else {
                    ll_newpwd_delete.setVisibility(View.GONE);
                }
            }
        });
        et_reNewPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View arg0, boolean isFocuse) {
                // TODO Auto-generated method stub
                if (isFocuse && flag_confimpwd_notnull) {
                    ll_confimpwd_delete.setVisibility(View.VISIBLE);
                } else {
                    ll_confimpwd_delete.setVisibility(View.GONE);
                }
            }
        });
        et_oldPass.addTextChangedListener(new EditChangedListener(FLAG_OLDPWD));
        et_newPass.addTextChangedListener(new EditChangedListener(FLAG_NEWPWD));
        et_reNewPass.addTextChangedListener(new EditChangedListener(FLAG_CONFIMPWD));
        ll_oldpwd_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                et_oldPass.getText().clear();
            }
        });
        ll_newpwd_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                et_newPass.getText().clear();
            }
        });
        ll_confimpwd_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                et_reNewPass.getText().clear();
            }
        });

    }

    public void attemptChange() {
        oldPass = et_oldPass.getText().toString();
        newPass1 = et_newPass.getText().toString();
        newPass2 = et_reNewPass.getText().toString();
        boolean cancel = false;
        if (TextUtils.isEmpty(oldPass)) {
            CommUtil.showToast(mContext, R.string.current_field_required, Toast.LENGTH_SHORT);
            focusView = et_oldPass;
            cancel = true;
        }
        if (!TextUtils.isEmpty(newPass1)
                && !TextUtils.isEmpty(newPass2)
                && !newPass1.equals(newPass2)) {
            /*txtConfirmPassword
                    .setError(getString(R.string.error_inconsistent_password));*/
            CommUtil.showToast(mContext, R.string.error_inconsistent_password, Toast.LENGTH_SHORT);
            focusView = et_newPass;
            cancel = true;
        }
        if (TextUtils.isEmpty(newPass2)) {
            /*txtNewPassword.setError(getString(R.string.error_field_required));*/
            CommUtil.showToast(mContext, R.string.prompt_confirm_new_password_with_tip, Toast.LENGTH_SHORT);
            focusView = et_reNewPass;
            cancel = true;
        }
        if (TextUtils.isEmpty(newPass1)) {
            /*txtNewPassword.setError(getString(R.string.error_field_required));*/
            CommUtil.showToast(mContext, R.string.prompt_new_password_with_tip, Toast.LENGTH_SHORT);
            focusView = et_newPass;
            cancel = true;
        } else if (newPass1.length() < Const.MIN_PASSWORD_LENGTH) {
            /*txtNewPassword
                    .setError(getString(R.string.error_short_limited_password));*/
            CommUtil.showToast(mContext, R.string.error_short_limited_password, Toast.LENGTH_SHORT);
            focusView = et_newPass;
            cancel = true;
        } else if (newPass1.matches("[0-9]+")
                || newPass1.matches("[a-zA-Z]+")
                || newPass1.matches("[^0-9a-zA-Z]+")) {
            /*txtNewPassword.setError(getString(R.string.error_password_limit));*/
            CommUtil.showToast(mContext, R.string.error_password_limit, Toast.LENGTH_SHORT);
            focusView = et_newPass;
            cancel = true;
        }
        if (cancel) {
            focusView.requestFocus();
        } else {
            MachTalkUtil.updateDoctorPwd(this,updatePwdcallback, PreferencesService.getInstance(mContext).getUserPhone(),
                    oldPass,newPass2,PreferencesService.getInstance(mContext).getTokenLong());
            //
        }

    }

    @Override
    public void onClick(View view) {

    }

    class EditChangedListener implements TextWatcher {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        private final int charMaxNum = 10;
        int flag;

        public EditChangedListener(int flag) {
            super();
            this.flag = flag;

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if (temp.length() > 0) {
                if (flag == FLAG_OLDPWD) {
                    flag_oldpwd_notnull = true;
                }
                if (flag == FLAG_NEWPWD) {
                    flag_newpwd_notnull = true;

                }
                if (flag == FLAG_NEWPWD) {
                    flag_newpwd_notnull = true;

                }
                if (flag == FLAG_CONFIMPWD) {
                    flag_confimpwd_notnull = true;

                }
            } else {
                if (flag == FLAG_OLDPWD) {
                    flag_oldpwd_notnull = false;
                }
                if (flag == FLAG_NEWPWD) {
                    flag_newpwd_notnull = false;
                }
                if (flag == FLAG_CONFIMPWD) {
                    flag_confimpwd_notnull = false;

                }
            }
            if (flag_oldpwd_notnull && flag_newpwd_notnull && flag_confimpwd_notnull) {
                btnsave.setClickable(true);
                btnsave.setBackgroundResource(R.drawable.btn_submit);
            } else {
                btnsave.setClickable(false);
                btnsave.setBackgroundResource(R.mipmap.button_gray);
            }
            if (flag_oldpwd_notnull && et_oldPass.isFocused()) {
                ll_oldpwd_delete.setVisibility(View.VISIBLE);
            } else {
                ll_oldpwd_delete.setVisibility(View.GONE);
            }
            if (flag_newpwd_notnull && et_newPass.isFocused()) {
                ll_newpwd_delete.setVisibility(View.VISIBLE);
            } else {
                ll_newpwd_delete.setVisibility(View.GONE);
            }
            if (flag_confimpwd_notnull && et_reNewPass.isFocused()) {
                ll_confimpwd_delete.setVisibility(View.VISIBLE);
            } else {
                ll_confimpwd_delete.setVisibility(View.GONE);
            }

        }
    }

    @Override
    protected void initialized() {
        // TODO Auto-generated method stub

    }
    StringCallback updatePwdcallback = new StringCallback() {
        @Override
        public void onSuccess(String str, okhttp3.Call call, okhttp3.Response response) {
            //上传成功
            Log.e("wangshu", "updatePwd===="+str);
            HttpResult param = EducationMSApplication.GSON.fromJson(str , HttpResult.class);
            if (param.getMsg() >= 0) {
                PreferencesService.getInstance(mContext).setIsLogin(false);
//            startActivity(InticareDActivity.class);
            }
                CommUtil.showToast(mContext, param.getMsgbox().toString(),  Toast.LENGTH_SHORT);

        }
        @Override
        public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
            //这里回调上传进度(该回调在主线程,可以直接更新ui)
        }
    };

}
